package org.scboy.dbms.controller;

import org.scboy.common.annotation.Menu;
import org.scboy.common.annotation.Permission;
import org.scboy.common.request.Result;
import org.scboy.dbms.service.BaseAppConfigService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/base/appconfig")
@Menu("微服务管理系统.微服务配置")
public class BaseAppConfigController {

	@Autowired
	BaseAppConfigService service;
	
	@PostMapping("page")
	@Permission("分页查询微服务配置")
	public Result page(@RequestBody Model params) {
		Page page=service.page(params);
		return Result.Success(page);
	}
	
	@PostMapping("add")
	@Permission("新增微服务配置")
	public Result add(@RequestBody Model params) {
		boolean result=service.add(params);
		if(result) {
			return Result.Success("APP_CONFIG_ADD_SUCCESS");
		}else{
			return Result.Error("APP_CONFIG_ADD_ERROR");
		}
	}
	
	@PutMapping("update")
	@Permission("修改微服务配置")
	public Result update(@RequestBody Model params) {
		boolean result=service.update(params);
		if(result) {
			return Result.Success("APP_CONFIG_UPDATE_SUCCESS");
		}else {
			return Result.Error("APP_CONFIG_UPDATE_ERROR");
		}
	}
	
	@DeleteMapping("delete")
	@Permission("删除微服务配置")
	public Result delete(String id) {
		boolean result=service.delete(id);
		if(result) {
			return Result.Success("APP_CONFIG_DELETE_SUCCESS");
		}else {
			return Result.Error("APP_CONFIG_DELETE_ERROR");
		}
	}
	
	@GetMapping("get")
	@Permission("获取微服务配置")
	public Result get(String id) {
		Model data=service.get(id);
		return Result.Success(data);
	}
	
	@PutMapping("active")
	@Permission("激活微服务配置")
	public Result active(String id) {
		boolean result=service.active(id);
		if(result) {
			return Result.Success("APP_CONFIG_ACTIVE_SUCCESS");
		}else {
			return Result.Error("APP_CONFIG_ACTIVE_ERROR");
		}
	}

	@PutMapping("upload")
	@Permission("激活微服务配置")
	public Result upload(String configId,String appServerId) {
		String result=service.uploadConfig(configId,appServerId);
		return Result.Success(result,0L);
	}
}
